Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Firestore] Add async counterpart for the addDocument method #13407

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

MojtabaHs
Copy link
Contributor

@MojtabaHs MojtabaHs commented Jul 26, 2024

The async version of the addDocument function exists only for Dictionary input, so I have added a codable version as well using the original existing methods.

  • The function signature is from the original; I just converted the completion parameter to async.
  • The documentation is from the original; I only added the Throws line from another existing function.
  • The implementation is from another existing function, but it uses the original addDocument which takes an encodable.

@paulb777
Copy link
Member

Thanks @MojtabaHs! I'd rather see this added to Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift since that file is already Swift and it would be closer to the non-async implementation.

@ncooke3 @wu-hui Do you know if it was an oversight that this is missing or some other reason?

MojtabaHs added a commit to MojtabaHs/firebase-ios-sdk that referenced this pull request Jul 27, 2024
continuation.resume(returning: document!)
}
}
} catch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catch looks redundant.

Copy link
Contributor Author

@MojtabaHs MojtabaHs Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It catches the possible thrown error by calling the original addDocument function (which seems only throwable by the underlying encoder). Since withCheckedThrowingContinuation takes a non-throwing closure, we can't just rethrow it.

Am I missing something here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. It looks like the problem is with the existing API above that both throws and does a callback with an error parameter.

Copy link
Contributor Author

@MojtabaHs MojtabaHs Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I was about to refactor it but stopped because it is part of the public API and would be backward-incompatible.

By the way, it's not clear to me what to return when encountering encoding errors.

I can open another PR for refactoring the existing API and discuss it further separately. Let me know your opinion.

Copy link
Contributor Author

@MojtabaHs MojtabaHs Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About this PR, how about moving this function back to the CollectionReference+AsyncAwait and using the existing API there like:

func addDocument<T: Encodable>(from value: T,
                               encoder: Firestore.Encoder = Firestore.Encoder()) async throws
-> DocumentReference {
  let encoded = try encoder.encode(value)
  return try await self.addDocument(data: encoded)
}

So no need for withCheckedThrowingContinuation at all.

Why move this to the other file? Because otherwise, this extension would depend on another extension.

Let me know your opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #9157 (comment) for a discussion about an edge case when the client is offline. This would result in the async method never returning.

Copy link
Contributor Author

@MojtabaHs MojtabaHs Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this method will depend on the existing async API. So, at the end of the day, we should either deprecate both of them or agree on the current existing logic.

@MojtabaHs MojtabaHs changed the title Add Firestore async counterparts [Firestore] Add async counterpart for the addDocument method Jul 28, 2024
@paulb777
Copy link
Member

@MojtabaHs Thanks for the PR. Given the open questions we're seeing, we want to a deeper review of the Firestore APIs and make sure we're being consistent with how we handle async and throwing. It may take us a while longer to move forward here.

@MojtabaHs
Copy link
Contributor Author

@paulb777 Let me know if there is anything I can do to help this progress.

@MojtabaHs
Copy link
Contributor Author

Any updates on this? There are other opportunities to introduce async counterparts to modernize the API, but I’m concerned that this decision could affect those efforts as well. I believe it’s important to address this first.

@ncooke3
Copy link
Member

ncooke3 commented Aug 12, 2024

Hi @MojtabaHs, no updates as of yet. This is something actively being looked into. I'll update when the direction is more clear.

@MojtabaHs
Copy link
Contributor Author

It's been a while, how can I assist you in moving forward with this progress?

@MojtabaHs
Copy link
Contributor Author

Rebased on the current main branch.

Any updates? It’s been a while, and I’m concerned about losing context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR]: Add async await to addDocument to collection with the parameter Codable
5 participants